package com.weijin.controller;

import com.weijin.model.entities.Vacation;
import com.weijin.model.from.OperateFrom;
import com.weijin.model.from.VacationFrom;
import com.weijin.model.vo.RestResultVo;
import com.weijin.service.IVacationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 请假相关接口
 *
 * @Author WeiJin
 * @Version 1.0
 * @Date 2025/3/28 19:30
 */
@Api(tags = "请假相关接口")
@RestController
@RequestMapping("/vacation")
public class VacationController {

    @Autowired
    private IVacationService vacationService;

    /**
     * 提交请假信息
     *
     * @param vacationFrom 请假信息入参
     * @return 请假信息
     */
    @ApiOperation("提交请假")
    @PostMapping("/submitVacation")
    public RestResultVo<Vacation> submitVacation(@RequestBody VacationFrom vacationFrom) {
        Vacation vacation = vacationService.submitVacation(vacationFrom);
        return RestResultVo.ok(vacation);
    }

    /**
     * 查询所有请假信息，仅供查看
     *
     * @return 所有请假信息
     */
    @ApiOperation("查询所有请假信息，仅供查看")
    @GetMapping("/queryAll")
    public RestResultVo<List<Vacation>> queryAll() {
        List<Vacation> vacations = vacationService.queryAll();
        return RestResultVo.ok(vacations);
    }

    /**
     * 根据用户id，查询下级为处理的请假
     *
     * @param userId 用户id
     * @return 下级为处理的请假
     */
    @ApiOperation("根据用户id，查询下级未处理的请假")
    @GetMapping("/queryLowerLevelUntreated")
    public RestResultVo<List<Vacation>> queryLowerLevelUntreated(String userId) {
        List<Vacation> vacations = vacationService.queryLowerLevelUntreated(userId);
        return RestResultVo.ok(vacations);
    }

    /**
     * 审批请假
     *
     * @param operateFrom 审批入参
     * @return 审批结果
     */
    @ApiOperation("审批请假")
    @PutMapping("/operateVacation")
    public RestResultVo<String> operateVacation(@RequestBody OperateFrom operateFrom) {
        int updated = vacationService.operateVacation(operateFrom);
        if (updated > 0) {
            return RestResultVo.ok(null, "操作成功");
        }
        return RestResultVo.failed("操作失败");
    }

    /**
     * 根据请假id获取请假信息 用于查看审批状况
     * @param id 请假id
     * @return 请假信息
     */
    @ApiOperation("根据请假id获取请假信息")
    @GetMapping("/queryVacationById")
    public RestResultVo<Vacation> queryVacationById(String id){
        Vacation vacation = vacationService.queryVacationById(id);
        return RestResultVo.ok(vacation);
    }
}
